﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    bool f;
    protected void Button1_Click(object sender, EventArgs e)
    {
        string a = (string)Session["ValidNums"];
        Session["usr"] = TextBox1.Text;
        string strConnection = "Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\\web.mdb";
        OleDbConnection objConnection = new OleDbConnection(strConnection);
        objConnection.Open();
        string sql = "select * from login where usr=@usr and passwd=@passwd";
        OleDbCommand cmd = new OleDbCommand(sql, objConnection);
        cmd.Parameters.Add("@usr", OleDbType.Char, 10);
        cmd.Parameters.Add("@passwd", OleDbType.Char, 10);
        cmd.Parameters["@usr"].Value = this.TextBox1.Text;
        cmd.Parameters["@passwd"].Value = this.TextBox2.Text;
        OleDbDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            string Username;
            string Password;
            Username = reader["usr"].ToString();
            Password = reader["passwd"].ToString();
            if ((Username.Trim() == TextBox1.Text) && (Password.Trim() == TextBox2.Text) && TextBox3.Text.Trim().ToUpper() == a.ToUpper())
            {
                f = true;
            }
           
        }
        else
        {
            if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "")
            {
                if (TextBox1.Text.Trim() == "")
                {
                    Response.Write("<script>alert('用户名不能为空')</script>");
                    TextBox3.Text = "";
                    return;
                }
                else if (TextBox2.Text.Trim() == "")
                {
                    Response.Write("<script>alert('密码不能为空')</script>");
                    TextBox3.Text = "";
                    return;
                }
                else if (TextBox3.Text.Trim() == "")
                {
                    Response.Write("<script>alert('验证码不能为空')</script>");

                    return;
                }
            }
            else 
                Response.Write("<script>alert('用户名不存在！或密码错误！！！')</script>");
            TextBox3.Text = "";
            return;
        }
        reader.Close();
        objConnection.Close();
        if (f == true)
        {
            Session["Name"] = TextBox1.Text;
            //Response.Write("<script>alert('登录成功！')</script>");
            Response.Write("<script>alert('登录成功!');window.location.href ='admin.aspx'</script>");
            Response.AddHeader("Refresh", "0");
            //Response.Redirect("http://user.qzone.qq.com/635725964/infocenter?ptsig=bgLTRZaZbIPtM*Ia24GXJL9jxQ-FKViERTVCZkWer-Y_");
            return;
        }
        else if (TextBox1.Text.Trim() == "")
        {
            Response.Write("<script>alert('用户名不能为空')</script>");
            TextBox3.Text = "";
            return;
        }
        else if (TextBox2.Text.Trim() == "")
        {
            Response.Write("<script>alert('密码不能为空')</script>");
            TextBox3.Text = "";
            return;

        }
        else if (TextBox3.Text.Trim() == "")
        {
            Response.Write("<script>alert('验证码不能为空')</script>");
            return;
        }
        else if (TextBox3.Text.Trim().ToUpper() != a.ToUpper())
        {
            Response.Write("<script>alert('验证码错误')</script>");
            TextBox3.Text = "";
            return;
        }
        else
            Response.Write("<script>alert('请检查用户或密码是否错误')</script>");
        TextBox3.Text = "";
        return;
    }

}